package com.jeomo.masterdata.provider.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.result.ResultCode;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.util.ExcelUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.masterdata.provider.request.CreateBrandRequest;
import com.jeomo.masterdata.provider.request.UpdateBrandRequest;
import com.jeomo.masterdata.provider.web.vo.BrandVo;
import com.jeomo.masterdata.starter.entity.Brand;
import com.jeomo.masterdata.starter.exception.IntegrityConstraintViolationException;
import com.jeomo.masterdata.starter.service.IBrandService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>品牌controller</p>
 *
 * @author : 崔贺
 * @date : 2020-08-28 13:56
 **/
@RequestMapping("/masterData/brand")
@RestController
public class BrandController extends BaseController {

    @Resource
    private IBrandService brandService;
    @Resource
    private ExcelUtil excelUtil;

    @PostMapping
    @ApiOperation("创建品牌")
    public CommonResult<String> create(@RequestBody @Validated CreateBrandRequest createBrandRequest) {
        try {
            Brand brand = BeanCopyUtil.copyProperties(createBrandRequest, Brand.class);
            brandService.create(brand);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("新增失败");
            }
        }
        return CommonResult.success("新增成功");
    }

    @DeleteMapping("/{code}")
    @ApiOperation("删除品牌")
    public CommonResult<String> remove(@PathVariable("code") String code) {
        Boolean canBrand = brandService.removeBrand(code);
        if (canBrand) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failure(ResultCode.DELETE_ERROR.msg());
        }
    }

    @PutMapping
    @ApiOperation("修改品牌")
    public CommonResult<String> update(@RequestBody UpdateBrandRequest updateBrandRequest) {
        try {
            brandService.updateById(BeanCopyUtil.copyProperties(updateBrandRequest, Brand.class));
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("修改失败");
            }
        }
        return CommonResult.success("修改成功");
    }

    @PostMapping("/page")
    @ApiOperation("获取品牌列表")
    public CommonResult<PageResponseVo<BrandVo>> page(@RequestBody PageRequest request) {
        PageResponseDto pageResponseDto = brandService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        return CommonResult.success(BeanCopyUtil.copyPageProperties(pageResponseDto, BrandVo.class));
    }

    @GetMapping("/{brandCode}")
    @ApiOperation("根据品牌号查询品牌实体")
    public CommonResult<BrandVo> queryByCode(@PathVariable("brandCode") String brandCode) {
        Brand brand = brandService.getById(brandCode);
        return CommonResult.success(BeanCopyUtil.copyProperties(brand, BrandVo.class));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有品牌")
    public CommonResult<List<BrandVo>> all() {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("start_flag", true);
        List<Brand> brands = brandService.list(wrapper);
        return CommonResult.success(BeanCopyUtil.copyListProperties(brands, BrandVo::new));
    }

    @PostMapping("/export")
    @ApiOperation("获取品牌列表")
    public void export(@RequestBody PageRequest request, HttpServletResponse response) {
        PageResponseDto pageResponseDto = brandService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        PageResponseVo pageResponseVo = BeanCopyUtil.copyPageProperties(pageResponseDto, BrandVo.class);
        List items = pageResponseVo.getItems();
        try {
            excelUtil.export(response, items);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
